package awk.jdk8.functionalInterface;

import java.util.Comparator;
import java.util.function.BinaryOperator;

public class OperatorTest {

	public static void main(String[] args) {
		//获取两个数之中较大的数
		Double operator = operator(BinaryOperator.maxBy(Comparator.naturalOrder()), 3.4, 5.6);
		System.out.println(operator);
		Value operator1 = operator(BinaryOperator.maxBy(Comparator.comparingInt((Value v) -> v.val)), new Value(1), new Value(2));
		System.out.println(operator1);
		//求和,输入两个参数对象，输出一个新的该类型对象
		Value operator2 = operator((a1, a2) -> new Value(a1.val + a2.val), new Value(1), new Value(2));
		System.out.println(operator2);

	}

	public static<T> T operator(BinaryOperator<T> binaryOperator, T a, T b) {
		return binaryOperator.apply(a, b);
	}

}

class Value {
	public int val;

	public Value(int val) {this.val = val;}
	public String toString() {return String.valueOf(val);}


	public static void main(String[] args) {
		//获取两个数之中较大的数
		Double operator = operator(BinaryOperator.maxBy(Comparator.naturalOrder()), 3.4, 5.6);
		System.out.println(operator);
		Value operator1 = operator(BinaryOperator.maxBy(Comparator.comparingInt((Value v) -> v.val)), new Value(1), new Value(2));
		System.out.println(operator1);
		//求和,输入两个参数对象，输出一个新的该类型对象
		Value operator2 = operator((a1, a2) -> new Value(a1.val + a2.val), new Value(1), new Value(2));
		System.out.println(operator2);

	}

	public static<T> T operator(BinaryOperator<T> binaryOperator, T a, T b) {
		return binaryOperator.apply(a, b);
	}

}

